//
//  JSVisitorLoginView.swift
//  JS weibo
//
//  Created by Jackie on 16/5/30.
//  Copyright © 2016年 Jackie. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol JSVisitorLoginViewDelegate:NSObjectProtocol {
    optional func userWilllogin()
    func userWillRegister()
}

class JSVisitorLoginView: UIView {
    weak var visitorDelegate:JSVisitorLoginViewDelegate?
    func updateUI(title:String,imageName:String,isHome:Bool = false) {
        if isHome {
            startAnimation()
        }else {
            largeHouse.hidden = true
            bringSubviewToFront(circleIcon)
        }
        circleIcon.image = UIImage(named: imageName)
        tipLabel.text = title
    }
    
    private func startAnimation() {
        let basic = CABasicAnimation(keyPath: "transform.rotation")
        basic.duration = 15
        basic.repeatCount = MAXFLOAT
        basic.toValue = 2 * M_PI
        basic.removedOnCompletion = false
        circleIcon.layer.addAnimation(basic,forKey:nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(circleIcon)
        addSubview(backIcon)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        circleIcon.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        largeHouse.snp_makeConstraints { (make) in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLabel.snp_makeConstraints { (make) in
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.height.equalTo(40)
        }
        loginBtn.snp_makeConstraints { (make) in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        registerBtn.snp_makeConstraints { (make) in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        backIcon.snp_makeConstraints { (make) in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        let value:CGFloat = 237.0 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        loginBtn.addTarget(self, action: #selector(JSVisitorLoginView.loginBtnDidClick), forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: #selector(JSVisitorLoginView.registerBtnDidClick), forControlEvents: .TouchUpInside)
        
    }
    
    @objc private func loginBtnDidClick() {
        visitorDelegate?.userWilllogin?()
    }
    
    @objc private func registerBtnDidClick() {
        visitorDelegate?.userWillRegister()
    }
    
    private lazy var backIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var circleIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var largeHouse:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var tipLabel:UILabel = {
        let l = UILabel()
        l.text = "关注一些人，回到这里看看有什么惊喜"
        l.textColor = UIColor.darkGrayColor()
        l.font = UIFont.systemFontOfSize(14)
        l.numberOfLines = 0
        l.preferredMaxLayoutWidth = 230
        l.sizeToFit()
        return l
    }()
    
    private lazy var loginBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        return btn
    }()
    
    private lazy var registerBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        return btn
    }()
    
}
